package cn.ly.net.tcp;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * tcp接收数据
 * a：创建接收ServerSocket的对象
 * b：监听客户端连接，返回一个对应的socket对象
 * c：获取输入流，显示
 * d：释放资源
 * @author liuyu
 *
 */

public class TcpServer {

	public static void main(String[] args) throws Exception {
		
		ServerSocket ss = new ServerSocket(8888);
		
		//监听，返回一个soeket对象
		Socket s = ss.accept();
		InputStream is = s.getInputStream();
		//new BufferedReader(new InputStreamReader(is));
		byte[] bys = new byte[2048];
		int len = is.read(bys);//阻塞
		String str = new String(bys);
		
		s.getInetAddress();
		
		
		System.out.println("server:" + str);

		//Thread.sleep(1000*10);
		
		OutputStream os = s.getOutputStream();
		os.write("收到".getBytes());
		
		
		s.close();
		
	}
	
}
